﻿package com.badibadi.activity;

import com.badibadi.fragment.ActivityOverViewFragment0;
import com.badibadi.fragment.ActivityMyActivityFragment0.getFragmentData;
import com.badibadi.fragment.ActivityOverViewFragment0.getFragmentData_1;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.mytools.CalendarTools;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.TabHostUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 
 * 活动预览界面
 * 
 * */ 
public class ActivityOverViewActivity extends BaseActivity implements
		OnClickListener {

	private FragmentManager manager;
	protected TabHost mTabHost;
	private Bundle bun;
	private ActivityOverViewFragment0 fragment0;
	private FragmentTransaction transaction;
	private ClueTypeModel clueTypeModel;
	protected TextView over_view_tital;
	private Button over_view_back, before_index;

	private Handler handler_main = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				over_view_tital.setText(msg.getData().getString("tital_name"));
				break;
			case 2:
//				   
				over_view_tital.setText(getResources().getString(
						R.string.ActivityOverview));
			default:
				break;
			}
		}
	};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.over_view);
		initGuanJianZi();
		init(1);
		initTabHost();
	}
    @Override
    protected void onPause() {
    	super.onPause();
    	ImageLoader.getInstance().clearDiscCache();
    	ImageLoader.getInstance().clearMemoryCache();
    }
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Constants.Activity_tital_handler = handler_main;

	}

	/**
	 * 这个是用于接收从查询处传过来的东西
	 */
	protected void initGuanJianZi() {
		// TODO Auto-generated method stub
		clueTypeModel = (ClueTypeModel) getIntent().getSerializableExtra(
				"CalendarActivity");
		over_view_tital = (TextView) findViewById(R.id.over_view_tital);
		if (clueTypeModel != null) {
			if (!StringUtil.isNullOrEmpty(clueTypeModel.getName()))
				over_view_tital.setText("" + clueTypeModel.getName());
		} else {
			over_view_tital.setText(getResources().getString(R.string.l_xb9));
		}
		this.findViewById(R.id.over_view_back).setOnClickListener(this);
		this.findViewById(R.id.before_index).setOnClickListener(this);
	}

	protected void init(int t) {
		// TODO Auto-generated method stub
		
		manager = getSupportFragmentManager();
		fragment0 = new ActivityOverViewFragment0();
		transaction = manager.beginTransaction();
		bun = new Bundle();
		bun.putInt("type", t);
		bun.putString("uid", Utils.getUid(ActivityOverViewActivity.this));
		if (clueTypeModel != null) {
			bun.putString("TYPE_id", clueTypeModel.getId());
		}
		fragment0.setArguments(bun);
		transaction.replace(R.id.activity_over_view_layout, fragment0);
		transaction.commit();
	}

	protected void initTabHost() {
		// TODO Auto-generated method stub
		mTabHost = (TabHost) findViewById(R.id.activity_over_view_tabhost);
		mTabHost.setup();
		mTabHost.addTab(mTabHost
				.newTabSpec("activity_over_view_tabhost one")
				.setIndicator(getResources().getString(R.string.Newest),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		mTabHost.addTab(mTabHost
				.newTabSpec("activity_over_view_tabhost two")
				.setIndicator(getResources().getString(R.string.Hottest),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		mTabHost.addTab(mTabHost
				.newTabSpec("activity_over_view_tabhost three")
				.setIndicator(getResources().getString(R.string.Nearby1),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		mTabHost.addTab(mTabHost
				.newTabSpec("activity_over_view_tabhost four")
				.setIndicator(getResources().getString(R.string.Deaeline),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab4));
		TabHostUtils.SetTabHost(this, mTabHost);
		mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				switch (mTabHost.getCurrentTab()) {
				case 0:
					if (clueTypeModel == null) {
						handler_main.sendEmptyMessage(2);
					}
					init(1);
					break;
				case 1:
					if (clueTypeModel == null) {
						handler_main.sendEmptyMessage(2);
					}
					init(2);
					break;
				case 2:
					if (clueTypeModel == null) {
						handler_main.sendEmptyMessage(2);
					}
					init(3);
					break;
				case 3:
					if (clueTypeModel == null) {
						handler_main.sendEmptyMessage(2);
					}
					init(4);
					break;
				}
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.over_view_back:
			// this.finish();
			fragment0.getSearchIsReturnUp_1(new getFragmentData_1() {

				@Override
				public void getIsReturnUp_1(boolean isReturn) {
					// TODO Auto-generated method stub
					clueTypeModel = null;
				}
			});
			break;
		case R.id.before_index:
			System.out.println("aaa"+Utils.getUid(ActivityOverViewActivity.this));
			
				Intent intent = new Intent(ActivityOverViewActivity.this,
						IndexActivity.class);
				startActivity(intent);
			
			break;
		default:
			break;
		}
	}
}
